/**
 * 
 */
package ua.com.infopulse.believeornevers.data;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import ua.at.tsvetkov.application.AppConfig;
import ua.at.tsvetkov.util.Log;
import ua.com.infopulse.believeornevers.Const;
import ua.com.infopulse.believeornevers.Key;

import android.content.Context;

/**
 * @author Alexandr.Tsvetkov
 */
public class RandomData {

	public String[] data;
	public ArrayList<Question> list;
	private int position = 0;

	/**
	 * @param context
	 * @param id
	 */
	public RandomData(Context context, int id) {
		data = context.getResources().getStringArray(id);
		list = new ArrayList<Question>();
		int length;
		if (AppConfig.getBoolean(Key.DEMO, false))
			length = Const.DEMO_LENGTH;
		else
			length = data.length;
		for (int i = 0; i < length; i++) {
			Question newQuestion = Question.getInstance(data[i]);
			if (newQuestion != null) {
				list.add(newQuestion);
			}
		}
		Collections.shuffle(list);
	}

	/**
	 * Return random question <a>
	 * 
	 * @return {@link Question}
	 * */
	public Question getNext() {
		Question question;
		// shuffle collection when all element processed
		Log.d("position:" + position + " from " + list.size());
		if (position < list.size()) {
			question = list.get(position);
		} else {
			Collections.shuffle(list);
			position = 0;
			question = list.get(position);
		}
		position++;
		return question;
	}

}
